package animation8.view.xiaomage.com.viewanimation8

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.AccelerateInterpolator
import kotlinx.android.synthetic.main.activity_main.*
import android.animation.Keyframe



class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_property_values_holder_begin.setOnClickListener{
            val rotationPropertyValuesHolder=
                    PropertyValuesHolder.ofFloat("rotation",
                            -30f,40f,50f,70f,-350f)
            val backgroundColorPropertyValuesHolder=
                    PropertyValuesHolder.ofInt("backgroundColor",
                            0xffffffff.toInt(), 0xffff00ff.toInt(),
                            0xffffff00.toInt(), 0xffffffff.toInt())
            val objectAnimator= ObjectAnimator.ofPropertyValuesHolder(
                    view,rotationPropertyValuesHolder,backgroundColorPropertyValuesHolder
            )
            objectAnimator.duration=5000
            objectAnimator.interpolator=AccelerateInterpolator()
            objectAnimator.start()
        }

        bt_property_values_holder_object_begin.setOnClickListener{
            val objectPropertyValuesHolder=
                    PropertyValuesHolder.ofObject("CharText",CharEvaluator(),
                            'A','Z')
            val objectAnimator=ObjectAnimator.ofPropertyValuesHolder(tv_title,
                    objectPropertyValuesHolder)
            objectAnimator.duration=5000
            objectAnimator.interpolator=AccelerateInterpolator()
            objectAnimator.start()
        }

        bt_property_values_holde_key_frame_begin.setOnClickListener{
            val frame0 = Keyframe.ofFloat(0f, 0f)
            val frame1 = Keyframe.ofFloat(0.1f, -20f)
            val frame2 = Keyframe.ofFloat(0.2f, 20f)
            val frame3 = Keyframe.ofFloat(0.3f, -20f)
            val frame4 = Keyframe.ofFloat(0.4f, 20f)
            val frame5 = Keyframe.ofFloat(0.5f, -20f)
            val frame6 = Keyframe.ofFloat(0.6f, 20f)
            val frame7 = Keyframe.ofFloat(0.7f, -20f)
            val frame8 = Keyframe.ofFloat(0.8f, 20f)
            val frame9 = Keyframe.ofFloat(0.9f, -20f)
            val frame10 = Keyframe.ofFloat(1f, 0f)
            val rotationKeyframePropertyValuesHolder=
                    PropertyValuesHolder.ofKeyframe("rotation",
                            frame0,
                            frame1,
                            frame2,
                            frame3,
                            frame4,
                            frame5,
                            frame6,
                            frame7,
                            frame8,
                            frame9,
                            frame10
                            )
            val scaleX0=Keyframe.ofFloat(0f,1f)
            val scaleX1=Keyframe.ofFloat(0.1f,1.2f)
            val scaleX2=Keyframe.ofFloat(0.2f,1.2f)
            val scaleX3=Keyframe.ofFloat(0.3f,1.2f)
            val scaleX4=Keyframe.ofFloat(0.4f,1.2f)
            val scaleX5=Keyframe.ofFloat(0.5f,1.2f)
            val scaleX6=Keyframe.ofFloat(0.6f,1.2f)
            val scaleX7=Keyframe.ofFloat(0.7f,1.2f)
            val scaleX8=Keyframe.ofFloat(0.8f,1.2f)
            val scaleX9=Keyframe.ofFloat(0.9f,1.2f)
            val scaleX10=Keyframe.ofFloat(1.0f,1.2f)
            val scaleXKeyframePropertyValuesHolder=
                    PropertyValuesHolder.ofKeyframe("scaleX",
                            scaleX0,
                            scaleX1,
                            scaleX2,
                            scaleX3,
                            scaleX4,
                            scaleX5,
                            scaleX6,
                            scaleX7,
                            scaleX8,
                            scaleX9,
                            scaleX10)

            val scaleY0=Keyframe.ofFloat(0f,1f)
            val scaleY1=Keyframe.ofFloat(0.1f,1.2f)
            val scaleY2=Keyframe.ofFloat(0.2f,1.2f)
            val scaleY3=Keyframe.ofFloat(0.3f,1.2f)
            val scaleY4=Keyframe.ofFloat(0.4f,1.2f)
            val scaleY5=Keyframe.ofFloat(0.5f,1.2f)
            val scaleY6=Keyframe.ofFloat(0.6f,1.2f)
            val scaleY7=Keyframe.ofFloat(0.7f,1.2f)
            val scaleY8=Keyframe.ofFloat(0.8f,1.2f)
            val scaleY9=Keyframe.ofFloat(0.9f,1.2f)
            val scaleY10=Keyframe.ofFloat(1.0f,1.2f)
            val scaleYKeyframePropertyValuesHolder=
                    PropertyValuesHolder.ofKeyframe("scaleY",
                            scaleY0,
                            scaleY1,
                            scaleY2,
                            scaleY3,
                            scaleY4,
                            scaleY5,
                            scaleY6,
                            scaleY7,
                            scaleY8,
                            scaleY9,
                            scaleY10
                    )

            val objectAnimator=ObjectAnimator.ofPropertyValuesHolder(img,
                    rotationKeyframePropertyValuesHolder,
                    scaleXKeyframePropertyValuesHolder,
                    scaleYKeyframePropertyValuesHolder)
            objectAnimator.duration=1000
            objectAnimator.start()
        }

        bt_property_values_holde_key_frame_char_begin.setOnClickListener{
            val charKeyframe0=Keyframe.ofObject(0f,'A')
            val charKeyframe1=Keyframe.ofObject(0.1f,'M')
            val charKeyframe2=Keyframe.ofObject(1f,'Z')
            val charPropertyValuesHolder=
                    PropertyValuesHolder.ofKeyframe("CharText",
                            charKeyframe0,
                            charKeyframe1,
                            charKeyframe2)
            charPropertyValuesHolder.setEvaluator(CharEvaluator())
            val objectAnimator=ObjectAnimator.ofPropertyValuesHolder(
                    tv_title,charPropertyValuesHolder
            )
            objectAnimator.duration=5000
            objectAnimator.start()
        }
    }
}
